﻿using System;
using System.Collections.Generic;
using CSharpExt.Threading;

namespace CSharpExt.Collections.Concurrent
{
    public class ConcurrentStack<T> : Stack<T>
    {
        #region Operations

        public void Clear()
        {
            lock (this)
            {
                base.Clear();
            }
        }

        public new void Push(T item)
        {
            lock (this)
            {
                base.Push(item);
                this.Signal();
            }
        }

        public new T Pop()
        {
            lock (this)
            {
                return base.Pop();
            }
        }

        public new T Peek()
        {
            lock (this)
            {
                return base.Peek();
            }
        }

        public T PopOrWait()
        {
            lock (this)
            {
                while (Count == 0)
                {
                    this.Wait(1000);
                }
                return base.Pop();
            }
        }

        public T PeekOrWait()
        {
            lock (this)
            {
                while (Count == 0)
                {
                    this.Wait(1000);
                }
                return base.Peek();
            }
        }

        #endregion
    }
}
